﻿using FOModels.ShortTermLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.ShortTermLoan
{
    public class ShortTermLoanDAL
    {
        private SQLDataAccess db;
        public ShortTermLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from ShortTermLoan order by ShortTermLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from ShortTermLoan sa join ShortTermLoanLang hl on sa.ShortTermLoanID = hl.ShortTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getShortTermLoanrelati(int CompanyID, int ShortTermLoanID, int langid)
        {
            return db.exeSelect("select * from ShortTermLoan ca join ShortTermLoanLang hl on ca.ShortTermLoanID = hl.ShortTermLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.ShortTermLoanID != " + ShortTermLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getShortTermLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from ShortTermLoan sa join ShortTermLoanLang hl on sa.ShortTermLoanID = hl.ShortTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableShortTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getShortTermLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from ShortTermLoan sa join ShortTermLoanLang hl on sa.ShortTermLoanID = hl.ShortTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableShortTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getShortTermLoanDefault(int langid)
        {
            return db.exeSelect("Select * from ShortTermLoan sa join ShortTermLoanLang hl on sa.ShortTermLoanID = hl.ShortTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableShortTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getShortTermLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from ShortTermLoan sa join ShortTermLoanLang hl on sa.ShortTermLoanID = hl.ShortTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableShortTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getShortTermLoanByID(int ShortTermLoanID, int langid)
        {
            return db.exeSelect("select * from ShortTermLoan ca join ShortTermLoanLang hl on ca.ShortTermLoanID = hl.ShortTermLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.ShortTermLoanID = " + ShortTermLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getShortTermLoanByIDManager(int ShortTermLoanID, int langid)
        {
            return db.exeSelect("select * from ShortTermLoan ca join ShortTermLoanLang hl on ca.ShortTermLoanID = hl.ShortTermLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.ShortTermLoanID = " + ShortTermLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateShortTermLoan(ShortTermLoanModels models)
        {
            return db.exeUpdate("update ShortTermLoan set ShortTermLoanName = N'" + models.ShortTermLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where ShortTermLoanID = " + models.ShortTermLoanID + "");
        }
        public int InsertShortTermLoan(ShortTermLoanModels models)
        {
            return db.exeUpdate("insert into ShortTermLoan values (N'" + models.ShortTermLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteShortTermLoan(int ShortTermLoanID)
        {
            return db.exeUpdate("Delete from ShortTermLoan where ShortTermLoanID = " + ShortTermLoanID + "");
        }
        public int UpdateShortTermLoanLang(ShortTermLoanModels models, int langid)
        {
            return db.exeUpdate("update ShortTermLoanLang set ShortTermLoanName = N'" + models.ShortTermLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where ShortTermLoanID = " + models.ShortTermLoanID + " and LangID=" + langid + "");
        }
        public int InsertShortTermLoanLang(ShortTermLoanModels models, int langid)
        {
            return db.exeUpdate("insert into ShortTermLoanLang values (" + models.ShortTermLoanID + "," + langid + ",N'" + models.ShortTermLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteShortTermLoanLang(int ShortTermLoanID)
        {
            return db.exeUpdate("Delete from ShortTermLoanLang where ShortTermLoanID = " + ShortTermLoanID + "");
        }

        public int DeleteShortTermLoanApply(int ShortTermLoanApplyID)
        {
            return db.exeUpdate("Delete from ShortTermLoanApply where ShortTermLoanApplyID = " + ShortTermLoanApplyID + "");
        }
        public DataTable getcontractbyid(int ShortTermLoanapplyid)
        {
            return db.exeSelect("select * from ShortTermLoanApply ca join [ShortTermLoan] c on ca.ShortTermLoanID = c.ShortTermLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.ShortTermLoanApplyID = " + ShortTermLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from ShortTermLoanApply ca join [ShortTermLoan] c on ca.ShortTermLoanID = c.ShortTermLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from ShortTermLoanApply ca join [ShortTermLoan] c on ca.ShortTermLoanID = c.ShortTermLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int ShortTermLoanApply(int ShortTermLoanID, ShortTermLoanApplyModels app)
        {
            return db.exeUpdate("insert into ShortTermLoanApply(ShortTermLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + ShortTermLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(ShortTermLoanApplyModels ShortTermLoan)
        {
            return db.exeUpdate("update ShortTermLoanApply set StatusID= " + ShortTermLoan.StatusID + " where ShortTermLoanApplyID = " + ShortTermLoan.ShortTermLoanApplyID + "");
        }
    }
}
